package com.maple.utils.widget.update


import android.text.TextUtils
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.GsonUtils
import com.maple.utils.util.LogUtils
import com.maple.utils.util.VersionUtils
import com.xuexiang.xupdate.entity.UpdateEntity
import com.xuexiang.xupdate.listener.IUpdateParseCallback
import com.xuexiang.xupdate.proxy.IUpdateParser

class CustomUpdateParser: IUpdateParser{

    private var isMustUpdate: Boolean = false
    private var isUpadate: Boolean = false
    override fun parseJson(json: String?): UpdateEntity {
        val entity: UpdateEntity = UpdateEntity()
        if(!TextUtils.isEmpty(json)) {
            try {
                /**
                 * setHasUpdate(result.hasUpdate)
                .setIsIgnorable(result.isIgnorable)
                .setVersionCode(result.versionCode)
                .setVersionName(result.versionName)
                .setUpdateContent(result.updateLog)
                .setDownloadUrl(result.apkUrl)
                .setSize(result.apkSize);
                 */


                LogUtils.logGGQ("====parseJson=====>>${json}")
                val appInfoEntity = GsonUtils.fromJson(json, AppInfoEntity::class.java)
                appInfoEntity?.body?.let { appInfo ->
                    isMustUpdate = appInfo.mustUpdate
                    isUpadate = VersionUtils.compareVersions(appInfo.vs,AppUtils.getAppVersionName())
                    LogUtils.logGGQ("====版本更新===>>>${json}")
                    entity.isHasUpdate = isUpadate
                    entity.setIsIgnorable(false)
                    entity.versionCode = appInfo.id
                    entity.versionName = appInfo.vs
                    entity.updateContent = appInfo.discription
                    entity.setDownloadUrl(appInfo.url)
                    entity.setIsSilent(false)
                    entity.setForce(false)
                    entity.setIsAutoInstall(true)
                }
            }catch (e: Exception) {
                e.printStackTrace()
                LogUtils.logGGQ("版本更新解析失败！！-->${e.message}")
            }
        }
        return entity
    }

    override fun parseJson(json: String?, callback: IUpdateParseCallback?) {
    }


    override fun isAsyncParser(): Boolean = false
}